<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>文件上传</title>
</head>
<body>
<div id="show_time"></div>
<label for="file"><img src="1.png" alt="" id="tmp_img"></label>
<input type="file" id="file" style="display: none">
<script>

    setInterval(function(){show_time.innerHTML = new Date();} , 1000);
    show_time.innerHTML = new Date();
    var file = document.getElementById('file');
    var tmp_img = document.getElementById('tmp_img');
    file.onchange = function ()
    {
        var xhr = new XMLHttpRequest();
        xhr.open('post' , '2-file.php' , false);
        // 实例化一个空的表单对象
        var fd = new FormData();

        fd.append('file' , this.files[0]);

        xhr.send(fd);
        xhr.onreadystatechange = function ()
        {
            if (xhr.readyState == 4 && xhr.status == 200)
            {
                tmp_img.src = xhr.responseText;
            }
        }
    }
</script>
</body>
</html>